package com.tnl.app.controller;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.time.DateUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.biz.hunter.db.entity.SchDic;
import com.biz.wechat.util.DateUtil;
import com.tnl.app.dao.CityDAO;
import com.tnl.app.dao.CompanyDAO;
import com.tnl.app.dao.EmployeeDAO;
import com.tnl.app.dao.SchDicDAO;
import com.tnl.app.entity.City;
import com.tnl.app.entity.Company;
import com.tnl.app.entity.CompanyOpt;
import com.tnl.app.entity.Employee;
import com.tnl.app.entity.Hunter;
import com.tnl.app.entity.SchJob;
import com.tnl.app.service.impl.SchJobService;
import com.tnl.app.service.impl.SchPersonService;
import com.tnl.app.vo.PageVo;
import com.tnl.base.web.JsonController;

@Controller
@RequestMapping("/schJob")
public class SchJobController extends JsonController {

	@Autowired
	private SchJobService schJobService;
	@Autowired
	private EmployeeDAO employeeDAO;
	@Autowired
	private CityDAO cityDAO;
	@Autowired
	private CompanyDAO companyDAO;
	@Autowired
	private SchDicDAO schDAO;
	
	@RequestMapping("/add")
	public void addSchJob(@RequestBody SchJob schjob,HttpServletResponse response) {
		schJobService.addJob(schjob);
		this.successJson(null, "success", response);
	}
	
	@RequestMapping("/get/{id}")
	public void getJobById(@PathVariable("id") String id,HttpServletResponse response) {
		SchJob j = schJobService.getById(id);
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		j.setCreateTimeStr(sdf.format(j.getCreateTime()));
		Employee emp = employeeDAO.getByEmpIdSimple(Integer.valueOf(j.getCreateBy()));
		if (emp.getCompanyId() != -1) {
			Company company = companyDAO.getById(emp.getCompanyId());
			j.setCompanyName(company != null ? company.getName():"");
			j.setCompanyAddr(company != null ? company.getAddress():"");
			j.setCompanyIcon(company != null ? company.getLogoIcon():"");
		}
		
		String[] arr = j.getCityId().split(",");
		if (arr!=null && arr.length>0) {
			String cityName = "";
			for (String cityId : arr) {
				City city = cityDAO.getById(Integer.valueOf(cityId));
				if (city != null) {
					cityName += city.getName();
					cityName += ",";
				}
			}
			j.setCityName(cityName.substring(0,cityName.length()-1));
		}
		switch (j.getDegree()) {
		case 1 :
			j.setDegreeName("本科"); break;
		case 2 :
			j.setDegreeName("硕士"); break;
		case 3 :
			j.setDegreeName("博士"); break;
		case 4 :
			j.setDegreeName("博士后"); break;
		}
		
		j.setSpecialtyName(schDAO.getName(j.getSpecialty().toString(), "spec"));
		if (StringUtils.isNotEmpty(j.getTalentSource())) {
			String[] schArr = j.getTalentSource().split(",");
			String schools = "";
			if (schArr != null && schArr.length>0) {
				for (String schid : schArr) {
					schools += schDAO.getName(schid, "school");
					schools += ",";
				}
			}
			j.setSchools(schools.substring(0, schools.length()-1));
		}
		
		this.successJson(j, "success", response);
	}
	
	@RequestMapping("/edit")
	public void editSchJob(@RequestBody SchJob schjob,HttpServletResponse response) {
		schJobService.updateJob(schjob);
		this.successJson(null, "success", response);
	}
	
	@RequestMapping("/del/{id}")
	public void delSchJob(@PathVariable("id") String id,HttpServletResponse response) {
		this.successJson(schJobService.delete(id), "success", response);
	}
	
	@RequestMapping("/page")
	public void jobList(Integer page, Integer size, SchJob schjob, HttpServletResponse response) {
		int totalNum = schJobService.getTotal(schjob);
        int pageNum = getPageNum(totalNum, size);
        int offset = (page - 1) * size;
		List<SchJob> list =  schJobService.page(offset,size,schjob);
		if(list !=null && list.size()>0) {
			list.forEach(j -> {
				SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
				j.setCreateTimeStr(sdf.format(j.getCreateTime()));
				Employee emp = employeeDAO.getByEmpId(Integer.valueOf(j.getCreateBy()));
				j.setCompanyName(emp != null ? emp.getCompanyName():"");
				String[] arr = j.getCityId().split(",");
				if (arr!=null && arr.length>0) {
					String cityName = "";
					for (String cityId : arr) {
						City city = cityDAO.getById(Integer.valueOf(cityId));
						if (city != null) {
							cityName += city.getName();
							cityName += ",";
						}
					}
					j.setCityName(cityName.substring(0,cityName.length()-1));
				}
				
				switch (j.getDegree()) {
				case 1 :
					j.setDegreeName("本科"); break;
				case 2 :
					j.setDegreeName("硕士"); break;
				case 3 :
					j.setDegreeName("博士"); break;
				case 4 :
					j.setDegreeName("博士后"); break;
				}
				j.setDescription("");
			});
		}
		this.successJson(new PageVo(list, pageNum), "success", response);
	}
	
	@RequestMapping("/getDic")
	public void jobList(String type,@RequestParam(value="flag",required=true,defaultValue="0")int flag, HttpServletResponse response) {
		List<SchDic> dic = schDAO.getAll(type,flag);
		if (dic!=null && dic.size()>0) {
			List<Map<String,String>> list = new ArrayList<Map<String,String>>();
			dic.forEach(d -> {
				Map<String,String> map = new HashMap<>();
				map.put("value", d.getId());
				map.put("name", d.getName());
				list.add(map);
			});
			this.successJson(list, "success", response);
		}else {
			this.errorJson(null, "暂无数据", response);
		}
	}
	
	@RequestMapping("/getcompanys") 
	public void getcompanys(String name, HttpServletResponse response) {
		List<Company> list = companyDAO.getOptions(name);
		this.successJson(list, "suceess", response);
	}
	
	@RequestMapping("/getcompanys2") 
	public void getcompanys2(String name,int i, HttpServletResponse response) {
		List<Company> list = companyDAO.getOptions2(name,i);
		this.successJson(list, "suceess", response);
	}
	
	@RequestMapping("/getcompanys1") 
	public void getcompanys1(String name, HttpServletResponse response) {
		List<Company> list = companyDAO.getOptions(name);
		List<CompanyOpt> opts = new ArrayList<>();
		if (list !=null && list.size()>0) {
			list.forEach(l -> {
				CompanyOpt opt = new CompanyOpt();
				opt.setId(l.getId());
				opt.setName(l.getName());
				opts.add(opt);
			});
		}
		this.successJson(opts, "suceess", response);
	}
}
